//
//  HomeViewController.m
//  Weibo
//
//  Created by student on 16/4/21.
//  Copyright © 2016年 Mcc. All rights reserved.
//

#import "HomeViewController.h"

#import "VisitorView.h"
#import "UIBarButtonItem+create.h"
#import "TitleButton.h"

#import "PopoverViewController.h"
#import "PopoverPresentationController.h"
#import "PopoverAnimator.h"
#import "QRCodeViewController.h"

#import "Status.h"
#import "StatusCell.h"
@interface HomeViewController ()
//转场动画代理对象
@property(nonatomic,strong)PopoverAnimator *popoverAnimator;
@property(nonatomic,strong)NSArray *status;
@end



// 自定义转场动画见:
//http://www.cnblogs.com/zanglitao/p/4167909.html

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(![[UserAccount sharedUserAccount] isLogined])
    {
        [self.visitorView setupVisitorInfo:YES imageName:@"visitordiscover_feed_image_house" message:@"关注一些人，回这里看看有什么惊喜"];
        
        return;
    }
    
    // 设置导航浪上的按钮
    [self setupNavi];
    
    // 监听菜单打开关闭通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:kNontificationPopoverWillShow object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:kNontificationPopoverWillDismiss object:nil];
    
   
    
    //    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.tableView registerClass:[StatusCell class] forCellReuseIdentifier:@"Cell"];
    [self loadData];
}
-(void)loadData
{
    __weak HomeViewController *weakSelf=self;
    [Status requestStatusWithSuccessBlock:^(NSArray *status) {
        weakSelf.status=status;
        [weakSelf.tableView reloadData];
    } failBlock:^(NSError *error) {
        //        NSLog(@"%@",[error localizedDescription]);
    }];
}
#pragma mark - 懒加载
-(PopoverAnimator *)popoverAnimator
{
    if (_popoverAnimator == nil)
    {
        _popoverAnimator=[[PopoverAnimator alloc]init];
        _popoverAnimator.presentFrame=CGRectMake(55, 56, 200, 350);
        
    }
    return _popoverAnimator;
}
-(void)change
{
    TitleButton *titleBtn=self.navigationItem.titleView;
    titleBtn.selected=!titleBtn.selected;
    
}
-(void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)setupNavi
{
    //1. 添加左右的按钮
    
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem createBarButtonItem:@"navigationbar_friendattention" target:self action:@selector(leftItemPressed:)];
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem createBarButtonItem:@"navigationbar_pop" target:self action:@selector(rightItemPressed:)];
    
    //2.创建标题按钮
    TitleButton *titleBtn=[[TitleButton alloc]init];
    [titleBtn setTitle:@"MCC" forState:UIControlStateNormal];

    [titleBtn addTarget:self action:@selector(titleBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [titleBtn sizeToFit];
    self.navigationItem.titleView=titleBtn;
}
-(void)leftItemPressed:(id)sender
{
    NSLog(@"%s",__func__);
}
-(void)rightItemPressed:(id)sender
{
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"QRCodeViewController" bundle:nil];
    UINavigationController *navi=sb.instantiateInitialViewController;
    [self presentViewController:navi animated:YES completion:nil];
    NSLog(@"%s",__func__);
}
-(void)titleBtnPressed:(UIButton *)sender
{
    NSLog(@"%s",__func__);
//    sender.selected=! sender.selected;
    
    //弹出菜单
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"PopoverViewController" bundle:nil];
   PopoverViewController *popVC=sb.instantiateInitialViewController;
    //    UIPopoverController *popVC = [sb instantiateViewControllerWithIdentifier:@"PopoverViewController"];// 通过StoryBoard ID
    
    // 采用自定义转场动画
    // 自定义转场动画
    popVC.modalPresentationStyle=UIModalPresentationCustom;
    popVC.transitioningDelegate=self.popoverAnimator;// 有谁来告诉他，谁来负责转场动画
    [self presentViewController:popVC animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.status.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" ];
      Status *status=self.status[indexPath.row];
    cell.status=status;
         return cell;
}

//预估单元格高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Status *status=self.status[indexPath.row];
    NSLog(@"高度:%g",status.cellHeight);
    return status.cellHeight;
}
@end
